
#include <BGridLayout>
#include <BHBoxLayout>
#include <BImage>
#include <BFileDialog>

#include "LabelPanel.h"

LabelPanel::LabelPanel(BWidget* parent) : BWidget(parent)
{
	_label = 0;

	BLabel* formLabel = new BLabel("form");
	_formComboBox = new BComboBox();
	_formComboBox->addItem("None", Form_None);
	_formComboBox->addItem("IconAlone", Form_IconAlone);
	_formComboBox->addItem("TextAlone", Form_TextAlone);
	_formComboBox->addItem("TextOnly", Form_TextOnly);
	_formComboBox->addItem("IconOnly", Form_IconOnly);
	_formComboBox->addItem("IconText", Form_IconText);
	_formComboBox->addItem("TextIcon", Form_TextIcon);
	_formComboBox->addItem("TextUnderIcon", Form_TextUnderIcon);
	_formComboBox->addItem("IconUnderText", Form_IconUnderText);

	BLabel* alignLabel = new BLabel("align");
	_alignButton = new BButton();
	_alignButton->setCheckable(true);
	_alignButton->setAlign(Align_LeftCenter);

	_alignWidget = new AlignWidget(this);
	_alignWidget->setVisible(false);
	_alignWidget->setSize(250, 250);

	BLabel* textLabel = new BLabel("text");
	_textEdit = new BLineEdit();
	
	BLabel* imageLabel = new BLabel("image");
	_imageButton = new BButton();
	_imageButton->setAlign(Align_VCenter);

	BLabel* imageSizeLabel = new BLabel("image-size");
	BHBoxLayout* imageSizeLayout = new BHBoxLayout();
	{
		_imageWidthSpinBox = new BSpinBox(0, 100);
		_imageHeightSpinBox = new BSpinBox(0, 100);

		imageSizeLayout->addWidget(_imageWidthSpinBox);
		imageSizeLayout->addWidget(_imageHeightSpinBox);
	}

	BGridLayout* gridLayout = new BGridLayout(this);
	gridLayout->setWidget(0, 0, formLabel);
	gridLayout->setWidget(0, 1, _formComboBox);
	gridLayout->setWidget(1, 0, alignLabel);
	gridLayout->setWidget(1, 1, _alignButton);
	gridLayout->setWidget(2, 0, textLabel);
	gridLayout->setWidget(2, 1, _textEdit);
	gridLayout->setWidget(3, 0, imageLabel);
	gridLayout->setWidget(3, 1, _imageButton);
	gridLayout->setWidget(4, 0, imageSizeLabel);
	gridLayout->setLayout(4, 1, imageSizeLayout);

	connect(this, Signal_AssetChanged, &LabelPanel::slotAssetChanged);

	connect(_formComboBox, Signal_ValueChanged, &LabelPanel::slotFormChanged);
	connect(_alignButton, Signal_Checked, &LabelPanel::slotAlignButtonChecked);
	connect(_alignWidget, Signal_AlignChanged, &LabelPanel::slotAlignChoosed);
	connect(_alignWidget, Signal_Focused, &LabelPanel::slotAlignWidgetFocused);
	connect(_textEdit, Signal_EditFinished, &LabelPanel::slotTextEdited);
	connect(_imageButton, Signal_Clicked, &LabelPanel::slotChooseImageFile);
	connect(_imageWidthSpinBox, Signal_EditFinished, &LabelPanel::slotImageSizeEdited);
	connect(_imageHeightSpinBox, Signal_EditFinished, &LabelPanel::slotImageSizeEdited);
}

void LabelPanel::slotAssetChanged()
{
	_label = this->asset();

	if (_label)
	{
		_alignWidget->setAlign(_label->align());

		_formComboBox->setValue(_label->form());
		_alignButton->setText(_alignWidget->alignText());
		_textEdit->setText(_label->text());
		if (BImage* image = _label->image())
		{
			_imageButton->setImage(image);
			_imageButton->setText(image->fileName());
		}
		else
		{
			_imageButton->setImage(0);
			_imageButton->setText(BString());
		}
		_imageWidthSpinBox->setValue(_label->imageSize().width());
		_imageHeightSpinBox->setValue(_label->imageSize().height());
	}
}

void LabelPanel::slotFormChanged(BObject* object, const BValue& value)
{
	Form form = value;
	_label->setForm(form);
}
void LabelPanel::slotAlignButtonChecked()
{
	if (_alignButton->checked())
	{
		BPoint point = this->mapFrom(_alignButton, BPoint(0, _alignButton->height()));
		_alignWidget->setPos(point);
		_alignWidget->popup();
	}
	else
	{
		_alignWidget->hide();
	}
}
void LabelPanel::slotAlignChoosed(BObject* object, const BValue& value)
{
	Align align = value;
	_label->setAlign(align);
	_alignButton->setText(_alignWidget->alignText());
	_alignButton->setChecked(false);
}
void LabelPanel::slotAlignWidgetFocused(BObject* object, const BValue& value)
{
	if (value == false)
	{
		_alignButton->setChecked(false);
	}
}
void LabelPanel::slotTextEdited(BObject* object, const BValue& value)
{
	const BString& text = value;
	_label->setText(text);
}
void LabelPanel::slotChooseImageFile()
{
	const BString& fileName = BFileDialog::OpenFile("Choose Image File", "data", "png, jpg, tiff, bmp");
	BImage* image = new BImage(fileName);
	_label->setImage(image);
	_imageButton->setImage(image);
	_imageButton->setText(fileName);
}
void LabelPanel::slotImageSizeEdited(BObject* object, const BValue& value)
{
	int imageWidth = _imageWidthSpinBox->value();
	int imageHeight = _imageHeightSpinBox->value();
	_label->setImageSize(BSize(imageWidth, imageHeight));
}

